/*
 * Copyright (C) 2009-2012 Geometer Plus <contact@geometerplus.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

package org.geometerplus.android.fbreader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.geometerplus.android.fbreader.api.ApiListener;
import org.geometerplus.android.fbreader.api.ApiServerImplementation;
import org.geometerplus.android.fbreader.api.PluginApi;
import org.geometerplus.android.fbreader.library.KillerCallback;
import org.geometerplus.android.fbreader.library.SQLiteBooksDatabase;
import org.geometerplus.android.fbreader.tips.TipsActivity;
import org.geometerplus.android.util.UIUtil;
import org.geometerplus.fbreader.bookmodel.BookModel;
import org.geometerplus.fbreader.fbreader.ActionCode;
import org.geometerplus.fbreader.fbreader.FBReaderApp;
import org.geometerplus.fbreader.library.Book;
import org.geometerplus.fbreader.library.Bookmark;
import org.geometerplus.fbreader.library.Library;
import org.geometerplus.fbreader.tips.TipsManager;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.text.hyphenation.ZLTextHyphenator;
import org.geometerplus.zlibrary.text.view.ZLTextView;
import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidActivity;
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidApplication;
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidLibrary;
import org.geometerplus.zlibrary.ui.android.view.AndroidFontUtil;

import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

public final class FBReader extends ZLAndroidActivity {
	public static final String BOOK_PATH_KEY = "BookPath";

	public static final int REQUEST_PREFERENCES = 1;
	public static final int REQUEST_BOOK_INFO = 2;
	public static final int REQUEST_CANCEL_MENU = 3;

	public static final int RESULT_DO_NOTHING = RESULT_FIRST_USER;
	public static final int RESULT_REPAINT = RESULT_FIRST_USER + 1;
	public static final int RESULT_RELOAD_BOOK = RESULT_FIRST_USER + 2;

	private int myFullScreenFlag;

	List<Bookmark> AllBooksBookmarks;
	private List<Bookmark> myThisBookBookmarks = new LinkedList<Bookmark>();
	private AlertDialog.Builder mAlertBuilder;
	private static final String PLUGIN_ACTION_PREFIX = "___";
	private final List<PluginApi.ActionInfo> myPluginActions = new LinkedList<PluginApi.ActionInfo>();
	private final BroadcastReceiver myPluginInfoReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			final ArrayList<PluginApi.ActionInfo> actions = getResultExtras(
					true).<PluginApi.ActionInfo> getParcelableArrayList(
					PluginApi.PluginInfo.KEY);
			if (actions != null) {
				synchronized (myPluginActions) {
					final FBReaderApp fbReader = (FBReaderApp) FBReaderApp
							.Instance();
					int index = 0;
					// clear actions
					while (index < myPluginActions.size()) {
						fbReader.removeAction(PLUGIN_ACTION_PREFIX + index++);
					}
					// add actions
					myPluginActions.addAll(actions);
					index = 0;
					for (PluginApi.ActionInfo info : myPluginActions) {
						fbReader.addAction(PLUGIN_ACTION_PREFIX + index++,
								new RunPluginAction(FBReader.this, fbReader,
										info.getId()));
					}
				}
			}
		}
	};

	// file comes from last activity
	@Override
	protected ZLFile fileFromIntent(Intent intent) {
		String filePath = intent.getStringExtra(BOOK_PATH_KEY);
		if (filePath == null) {
			final Uri data = intent.getData();
			if (data != null) {
				filePath = data.getPath();
			}
		}
		return filePath != null ? ZLFile.createFileByPath(filePath) : null;
	}

	@Override
	protected Runnable getPostponedInitAction() {
		return new Runnable() {
			public void run() {
				runOnUiThread(new Runnable() {
					public void run() {
						new TipRunner().start();
						DictionaryUtil.init(FBReader.this);
					}
				});
			}
		};
	}

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		mAlertBuilder = new AlertDialog.Builder(this);
		final FBReaderApp fbReader = (FBReaderApp) FBReaderApp.Instance();
		final ZLAndroidLibrary zlibrary = (ZLAndroidLibrary) ZLibrary
				.Instance();
		myFullScreenFlag = zlibrary.ShowStatusBarOption.getValue() ? 0
				: WindowManager.LayoutParams.FLAG_FULLSCREEN;
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				myFullScreenFlag);

		if (fbReader.getPopupById(TextSearchPopup.ID) == null) {

			new TextSearchPopup(fbReader);
		}
		if (fbReader.getPopupById(NavigationPopup.ID) == null) {
			new NavigationPopup(fbReader, FBReader.this);
		}
		if (fbReader.getPopupById(NavigationPopupTop.ID) == null) {
			new NavigationPopupTop(fbReader);
		}
		if (fbReader.getPopupById(TextSearchPopupTop.ID) == null) {
			new TextSearchPopupTop(fbReader);
		}
		if (fbReader.getPopupById(SelectionPopup.ID) == null) {
			new SelectionPopup(fbReader);
		}

		fbReader.addAction(ActionCode.SHOW_LIBRARY, new ShowLibraryAction(this,
				fbReader));
		fbReader.addAction(ActionCode.SHOW_PREFERENCES,
				new ShowPreferencesAction(this, fbReader));
		fbReader.addAction(ActionCode.SHOW_BOOK_INFO, new ShowBookInfoAction(
				this, fbReader));
		fbReader.addAction(ActionCode.SHOW_TOC, new ShowTOCAction(this,
				fbReader));
		fbReader.addAction(ActionCode.SHOW_BOOKMARKS, new ShowBookmarksAction(
				this, fbReader));
		fbReader.addAction(ActionCode.SHOW_NETWORK_LIBRARY,
				new ShowNetworkLibraryAction(this, fbReader));

		fbReader.addAction(ActionCode.TURN_PAGE_FORWARD,
				new ShowTurnPageAction(this, fbReader, true));
		fbReader.addAction(ActionCode.TURN_PAGE_BACK, new ShowTurnPageAction(
				this, fbReader, false));

		fbReader.addAction(ActionCode.SHOW_MENU, new ShowMenuAction(this,
				fbReader));
		fbReader.addAction(ActionCode.SHOW_NAVIGATION,
				new ShowNavigationAction(this, fbReader));
		fbReader.addAction(ActionCode.SEARCH, new SearchAction(this, fbReader));
		fbReader.addAction(ActionCode.FIND_NEXT, new FindNextAction(this,
				fbReader));
		fbReader.addAction(ActionCode.FIND_PREVIOUS, new FindPreviousAction(
				this, fbReader));
		fbReader.addAction(ActionCode.SHARE_BOOK, new ShareBookAction(this,
				fbReader));

		fbReader.addAction(ActionCode.SHOW_CANCEL, new CancelSearchAction(this,
				fbReader));
		fbReader.addAction(ActionCode.SELECTION_SHOW_PANEL,
				new SelectionShowPanelAction(this, fbReader));
		fbReader.addAction(ActionCode.SELECTION_HIDE_PANEL,
				new SelectionHidePanelAction(this, fbReader));
		fbReader.addAction(ActionCode.SELECTION_COPY_TO_CLIPBOARD,
				new SelectionCopyAction(this, fbReader));
		fbReader.addAction(ActionCode.SELECTION_SHARE,
				new SelectionShareAction(this, fbReader));
		fbReader.addAction(ActionCode.SELECTION_TRANSLATE,
				new SelectionTranslateAction(this, fbReader));
		fbReader.addAction(ActionCode.SELECTION_BOOKMARK,
				new SelectionBookmarkAction(this, fbReader));

		fbReader.addAction(ActionCode.PROCESS_HYPERLINK,
				new ProcessHyperlinkAction(this, fbReader));

		fbReader.addAction(ActionCode.SHOW_CANCEL_MENU,
				new ShowCancelMenuAction(this, fbReader));

		// fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_SYSTEM, new
		// SetScreenOrientationAction(this, fbReader,
		// ZLibrary.SCREEN_ORIENTATION_SYSTEM));
		// fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_SENSOR, new
		// SetScreenOrientationAction(this, fbReader,
		// ZLibrary.SCREEN_ORIENTATION_SENSOR));
		fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_PORTRAIT,
				new SetScreenOrientationAction(this, fbReader,
						ZLibrary.SCREEN_ORIENTATION_PORTRAIT));
		// fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_LANDSCAPE, new
		// SetScreenOrientationAction(this, fbReader,
		// ZLibrary.SCREEN_ORIENTATION_LANDSCAPE));
		if (ZLibrary.Instance().supportsAllOrientations()) {
			// fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_REVERSE_PORTRAIT,
			// new SetScreenOrientationAction(this, fbReader,
			// ZLibrary.SCREEN_ORIENTATION_REVERSE_PORTRAIT));
			// fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_REVERSE_LANDSCAPE,
			// new SetScreenOrientationAction(this, fbReader,
			// ZLibrary.SCREEN_ORIENTATION_REVERSE_LANDSCAPE));
		}
		fbReader.runAction(ActionCode.SET_SCREEN_ORIENTATION_PORTRAIT);
		DictionaryUtil.init(FBReader.this);

	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		final ZLAndroidLibrary zlibrary = (ZLAndroidLibrary) ZLibrary
				.Instance();
		if (!zlibrary.isKindleFire()
				&& !zlibrary.ShowStatusBarOption.getValue()) {
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		}
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public void onOptionsMenuClosed(Menu menu) {
		super.onOptionsMenuClosed(menu);
		final ZLAndroidLibrary zlibrary = (ZLAndroidLibrary) ZLibrary
				.Instance();
		if (!zlibrary.isKindleFire()
				&& !zlibrary.ShowStatusBarOption.getValue()) {
			getWindow().clearFlags(
					WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		final ZLAndroidLibrary zlibrary = (ZLAndroidLibrary) ZLibrary
				.Instance();
		if (!zlibrary.isKindleFire()
				&& !zlibrary.ShowStatusBarOption.getValue()) {
			getWindow().clearFlags(
					WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		final Uri data = intent.getData();
		final FBReaderApp fbReader = (FBReaderApp) FBReaderApp.Instance();
		if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
			super.onNewIntent(intent);
		} else if (Intent.ACTION_VIEW.equals(intent.getAction())
				&& data != null && "fbreader-action".equals(data.getScheme())) {
			fbReader.runAction(data.getEncodedSchemeSpecificPart(),
					data.getFragment());
		} else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			Log.e("ACTION_SEARCH", "1111111111");
			final String pattern = intent.getStringExtra(SearchManager.QUERY);
			final Runnable runnable = new Runnable() {
				public void run() {

					final TextSearchPopup popup = (TextSearchPopup) fbReader
							.getPopupById(TextSearchPopup.ID);
					popup.initPosition();
					fbReader.TextSearchPatternOption.setValue(pattern);
					if (fbReader.getTextView().search(pattern, true, false,
							false, false) != 0) {
						runOnUiThread(new Runnable() {
							public void run() {
								Log.e("ACTION_SEARCH", "222222222");
								fbReader.showPopup(popup.getId());
							}
						});
					} else {
						Log.e("FBreader", "wwwwwwwwww");
						runOnUiThread(new Runnable() {
							public void run() {
								UIUtil.showErrorMessage(FBReader.this,
										"textNotFound");
								popup.StartPosition = null;
							}
						});
					}
				}
			};
			UIUtil.wait("search", runnable, this);
		} else {
			super.onNewIntent(intent);
		}

		// 隐藏阅读界面菜单
		fbReader.hideAllPopup();
		fbReader.getViewWidget().reset();
		fbReader.getViewWidget().repaint();
	}

	public boolean searchBackOrForward(boolean isBack) {
		final FBReaderApp fbReader = (FBReaderApp) FBReaderApp.Instance();
		String pattern = fbReader.TextSearchPatternOption.getValue();
		final String dialogTitle = this.getText(R.string.text_not_found)
				.toString();
		if (isBack == true) {
			if (fbReader.getTextView()
					.search(pattern, true, false, true, false) != 0) {
				runOnUiThread(new Runnable() {
					public void run() {
					}
				});
				return true;
			} else {
				runOnUiThread(new Runnable() {
					public void run() {
						showNoTextDialog(dialogTitle);
					}
				});
				return false;
			}
		} else {
			if (fbReader.getTextView().search(pattern, true, false, false,
					false) != 0) {
				runOnUiThread(new Runnable() {
					public void run() {
					}
				});
				return true;
			} else {
				runOnUiThread(new Runnable() {
					public void run() {
						showNoTextDialog(dialogTitle);
					}
				});
				return false;
			}
		}

	}

	public void showNoTextDialog(String text) {
		mAlertBuilder.setTitle(text);
		AlertDialog alert = mAlertBuilder.create();
		alert.setButton(AlertDialog.BUTTON_POSITIVE, "Dismiss",
				(DialogInterface.OnClickListener) null);
		alert.show();
	}

	@Override
	public void onStart() {
		super.onStart();

		initPluginActions();

		final ZLAndroidLibrary zlibrary = (ZLAndroidLibrary) ZLibrary
				.Instance();

		final int fullScreenFlag = zlibrary.ShowStatusBarOption.getValue() ? 0
				: WindowManager.LayoutParams.FLAG_FULLSCREEN;
		if (fullScreenFlag != myFullScreenFlag) {
			finish();
			startActivity(new Intent(this, getClass()));
		}

		SetScreenOrientationAction.setOrientation(this,
				zlibrary.OrientationOption.getValue());

		final FBReaderApp fbReader = (FBReaderApp) FBReaderApp.Instance();
		final RelativeLayout root = (RelativeLayout) findViewById(R.id.root_view);
		((PopupPanel) fbReader.getPopupById(TextSearchPopup.ID)).setPanelInfo(
				this, root);
		((PopupPanel) fbReader.getPopupById(NavigationPopup.ID)).setPanelInfo(
				this, root);
		((PopupPanel) fbReader.getPopupById(NavigationPopupTop.ID))
				.setPanelInfo(this, root);
		((PopupPanel) fbReader.getPopupById(TextSearchPopupTop.ID))
				.setPanelInfo(this, root);
		((PopupPanel) fbReader.getPopupById(SelectionPopup.ID)).setPanelInfo(
				this, root);
	}

	private void initPluginActions() {
		final FBReaderApp fbReader = (FBReaderApp) FBReaderApp.Instance();
		synchronized (myPluginActions) {
			int index = 0;
			while (index < myPluginActions.size()) {
				fbReader.removeAction(PLUGIN_ACTION_PREFIX + index++);
			}
			myPluginActions.clear();
		}

		sendOrderedBroadcast(new Intent(PluginApi.ACTION_REGISTER), null,
				myPluginInfoReceiver, null, RESULT_OK, null, null);
	}

	private class TipRunner extends Thread {
		TipRunner() {
			setPriority(MIN_PRIORITY);
		}

		public void run() {
			final TipsManager manager = TipsManager.Instance();
			switch (manager.requiredAction()) {
			case Initialize:
				startActivity(new Intent(TipsActivity.INITIALIZE_ACTION, null,
						FBReader.this, TipsActivity.class));
				break;
			case Show:
				startActivity(new Intent(TipsActivity.SHOW_TIP_ACTION, null,
						FBReader.this, TipsActivity.class));
				break;
			case Download:
				manager.startDownloading();
				break;
			case None:
				break;
			}
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		try {
			sendBroadcast(new Intent(getApplicationContext(),
					KillerCallback.class));
		} catch (Throwable t) {
		}
		PopupPanel.restoreVisibilities(FBReaderApp.Instance());
		ApiServerImplementation.sendEvent(this,
				ApiListener.EVENT_READ_MODE_OPENED);
	}

	@Override
	public void onStop() {
		ApiServerImplementation.sendEvent(this,
				ApiListener.EVENT_READ_MODE_CLOSED);
		PopupPanel.removeAllWindows(FBReaderApp.Instance(), this);
		super.onStop();
	}

	@Override
	protected FBReaderApp createApplication() {
		if (SQLiteBooksDatabase.Instance() == null) {
			new SQLiteBooksDatabase(this, "READER");
		}
		return new FBReaderApp();
	}

	@Override
	public boolean onSearchRequested() {
		if (((TextSearchPopupTop) FBReaderApp.Instance().getPopupById(
				TextSearchPopupTop.ID)).myWindow == null
				|| ((TextSearchPopupTop) FBReaderApp.Instance().getPopupById(
						TextSearchPopupTop.ID)).myWindow.getVisibility() == View.GONE) {
			((TextSearchPopupTop) FBReaderApp.Instance().getPopupById(
					TextSearchPopupTop.ID)).runSearch();
			showKeyboard();
		}
		return true;
	}

	void showKeyboard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null)
			imm.showSoftInput(((TextSearchPopupTop) FBReaderApp.Instance()
					.getPopupById(TextSearchPopupTop.ID)).searchText, 0);
	}

	void hideKeyboard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null)
			imm.hideSoftInputFromWindow(
					((TextSearchPopupTop) FBReaderApp.Instance().getPopupById(
							TextSearchPopupTop.ID)).searchText.getWindowToken(),
					0);
	}

	public void showSelectionPanel() {
		final FBReaderApp fbReader = (FBReaderApp) FBReaderApp.Instance();
		final ZLTextView view = fbReader.getTextView();
		((SelectionPopup) fbReader.getPopupById(SelectionPopup.ID)).move(
				view.getSelectionStartY(), view.getSelectionEndY());
		// fbReader.showPopup(SelectionPopup.ID);
	}

	public void hideSelectionPanel() {
		final FBReaderApp fbReader = (FBReaderApp) FBReaderApp.Instance();
		final FBReaderApp.PopupPanel popup = fbReader.getActivePopup();
		if (popup != null && popup.getId() == SelectionPopup.ID) {
			fbReader.hideActivePopup();
		}
	}

	private void onPreferencesUpdate(int resultCode) {
		final FBReaderApp fbReader = (FBReaderApp) FBReaderApp.Instance();
		switch (resultCode) {
		case RESULT_DO_NOTHING:
			break;
		case RESULT_REPAINT: {
			AndroidFontUtil.clearFontCache();
			final BookModel model = fbReader.Model;
			if (model != null) {
				final Book book = model.Book;
				if (book != null) {
					book.reloadInfoFromDatabase();
					ZLTextHyphenator.Instance().load(book.getLanguage());
				}
			}
			fbReader.clearTextCaches();
			fbReader.getViewWidget().repaint();
			break;
		}
		case RESULT_RELOAD_BOOK:
			fbReader.reloadBook();
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_PREFERENCES:
		case REQUEST_BOOK_INFO:
			onPreferencesUpdate(resultCode);
			break;
		case REQUEST_CANCEL_MENU:
			((FBReaderApp) FBReaderApp.Instance())
					.runCancelAction(resultCode - 1);
			break;
		}
	}

	public void navigate() {
		((NavigationPopupTop) FBReaderApp.Instance().getPopupById(
				NavigationPopupTop.ID)).runNavigation();
		((NavigationPopup) FBReaderApp.Instance().getPopupById(
				NavigationPopup.ID)).runNavigation();

		// 隐藏搜索相关
		((TextSearchPopup) FBReaderApp.Instance().getPopupById(
				TextSearchPopup.ID)).hide_();

		// 书签相关
		updateNavigationPopupTop();
	}

	public void hideNavigationPopupTop() {
		final FBReaderApp fbreader = (FBReaderApp) FBReaderApp.Instance();
		// 先隐藏阅读界面菜单
		fbreader.hideAllPopup();
		fbreader.getViewWidget().reset();
		fbreader.getViewWidget().repaint();
	}

	public void updateNavigationPopupTop() {
		final FBReaderApp fbreader = (FBReaderApp) FBReaderApp.Instance();
		// load this book marks
		AllBooksBookmarks = Library.Instance().allBookmarks();
		Collections.sort(AllBooksBookmarks, new Bookmark.ByTimeComparator());
		if (fbreader.Model != null) {
			final long bookId = fbreader.Model.Book.getId();
			myThisBookBookmarks.clear();
			for (Bookmark bookmark : AllBooksBookmarks) {
				if (bookmark.getBookId() == bookId) {
					myThisBookBookmarks.add(bookmark);
				}
			}
		}

		final Bookmark currentBookmark = fbreader.addBookmark(20, true);
		Boolean exist = false;
		if (currentBookmark != null) {
			for (Bookmark b : myThisBookBookmarks) {
				if (b.getText().equalsIgnoreCase(currentBookmark.getText())) {
					exist = true;
					break;
				}
			}
			((NavigationPopupTop) FBReaderApp.Instance().getPopupById(
					NavigationPopupTop.ID)).setMarkonFlag(exist);
			((NavigationPopupTop) FBReaderApp.Instance().getPopupById(
					NavigationPopupTop.ID)).update();
		}
	}

	public void addBookMark() {
		final FBReaderApp fbreader = (FBReaderApp) FBReaderApp.Instance();
		final Bookmark currentBookmark = fbreader.addBookmark(20, true);
		Bookmark bm = currentBookmark;
		Boolean exist = false;
		if (currentBookmark != null) {
			for (Bookmark b : myThisBookBookmarks) {
				if (b.getText().equalsIgnoreCase(currentBookmark.getText())) {
					exist = true;
					bm = b;
					break;
				}
			}
			if (!exist) {
				currentBookmark.save();
				myThisBookBookmarks.add(currentBookmark);
				((NavigationPopupTop) FBReaderApp.Instance().getPopupById(
						NavigationPopupTop.ID)).setMarkonFlag(true);
				Toast.makeText(FBReader.this, "添加书签成功", 1000).show();
			} else {
				bm.delete();
				currentBookmark.delete();
				myThisBookBookmarks.remove(bm);
				myThisBookBookmarks.remove(currentBookmark);
				((NavigationPopupTop) FBReaderApp.Instance().getPopupById(
						NavigationPopupTop.ID)).setMarkonFlag(false);
				Toast.makeText(FBReader.this, "删除书签", 1000).show();
			}
			((NavigationPopupTop) FBReaderApp.Instance().getPopupById(
					NavigationPopupTop.ID)).update();
		}
	}

	private Menu addSubMenu(Menu menu, String id) {
		final ZLAndroidApplication application = (ZLAndroidApplication) getApplication();
		return application.myMainWindow.addSubMenu(menu, id);
	}

	private void addMenuItem(Menu menu, String actionId, String name) {
		final ZLAndroidApplication application = (ZLAndroidApplication) getApplication();
		application.myMainWindow.addMenuItem(menu, actionId, null, name);
	}

	private void addMenuItem(Menu menu, String actionId, int iconId) {
		final ZLAndroidApplication application = (ZLAndroidApplication) getApplication();
		application.myMainWindow.addMenuItem(menu, actionId, iconId, null);
	}

	private void addMenuItem(Menu menu, String actionId) {
		final ZLAndroidApplication application = (ZLAndroidApplication) getApplication();
		application.myMainWindow.addMenuItem(menu, actionId, null, null);
	}

	// 绑定阅读界面imagebutton动作
	public void addImageButton(ImageButton button, String actionId) {
		final ZLAndroidApplication application = (ZLAndroidApplication) getApplication();
		final float[] BT_SELECTED = new float[] { 2, 0, 0, 0, 2, 0, 2, 0, 0, 2,
				0, 0, 2, 0, 2, 0, 0, 0, 1, 0 };
		// button.setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
		// button.setBackgroundDrawable(button.getBackground());
		application.myMainWindow.addImageButton(button, actionId, null, null);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		// addMenuItem(menu, ActionCode.SHOW_LIBRARY,
		// R.drawable.ic_menu_library);
		// addMenuItem(menu, ActionCode.SHOW_NETWORK_LIBRARY,
		// R.drawable.ic_menu_networklibrary);
		addMenuItem(menu, ActionCode.SHOW_TOC, R.drawable.ic_menu_toc);
		addMenuItem(menu, ActionCode.SHOW_BOOKMARKS,
				R.drawable.ic_menu_bookmarks);
		// addMenuItem(menu, ActionCode.SWITCH_TO_NIGHT_PROFILE,
		// R.drawable.ic_menu_night);
		// addMenuItem(menu, ActionCode.SWITCH_TO_DAY_PROFILE,
		// R.drawable.ic_menu_day);
		addMenuItem(menu, ActionCode.SEARCH, R.drawable.ic_menu_search);
		// addMenuItem(menu, ActionCode.SHARE_BOOK, R.drawable.ic_menu_search);
		// addMenuItem(menu, ActionCode.SHOW_PREFERENCES);
		// addMenuItem(menu, ActionCode.SHOW_BOOK_INFO);
		// final Menu subMenu = addSubMenu(menu, "screenOrientation");
		// addMenuItem(subMenu, ActionCode.SET_SCREEN_ORIENTATION_SYSTEM);
		// addMenuItem(subMenu, ActionCode.SET_SCREEN_ORIENTATION_SENSOR);
		// addMenuItem(subMenu, ActionCode.SET_SCREEN_ORIENTATION_PORTRAIT);
		// addMenuItem(subMenu, ActionCode.SET_SCREEN_ORIENTATION_LANDSCAPE);
		// if (ZLibrary.Instance().supportsAllOrientations()) {
		// addMenuItem(subMenu,
		// ActionCode.SET_SCREEN_ORIENTATION_REVERSE_PORTRAIT);
		// addMenuItem(subMenu,
		// ActionCode.SET_SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
		// }
		addMenuItem(menu, ActionCode.INCREASE_FONT);
		addMenuItem(menu, ActionCode.DECREASE_FONT);
		// addMenuItem(menu, ActionCode.SHOW_NAVIGATION);
		synchronized (myPluginActions) {
			int index = 0;
			for (PluginApi.ActionInfo info : myPluginActions) {
				if (info instanceof PluginApi.MenuActionInfo) {
					addMenuItem(menu, PLUGIN_ACTION_PREFIX + index++,
							((PluginApi.MenuActionInfo) info).MenuItemName);
				}
			}
		}

		final ZLAndroidApplication application = (ZLAndroidApplication) getApplication();
		application.myMainWindow.refresh();

		return true;
	}

}
